package com.egoo.sys.common;

import java.util.ArrayList;
import java.util.List;
/**
 * 构建层级关系
*@Author Egoo
*@Date 2022/8/6 20:52
*/
public class TreeNodeBuilder {
    public static List<TreeNode> build(List<TreeNode> treeNodes, Integer topPid) {
        List<TreeNode> nodes = new ArrayList<TreeNode>();
        //遍历判断是否为顶级节点 遍历顶级节点的子节点 并加入将其设置为顶节点或子节点
        for (TreeNode n1 : treeNodes) {
            if (n1.getPid()==topPid){
                nodes.add(n1);
            }
            for (TreeNode n2 : treeNodes) {
                if (n1.getId()==n2.getPid()){
                    n1.getChildren().add(n2);
                }
            }
        }
        return nodes;
    }
}
